package org.jeecg.modules.wangyue.controller.sys;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.wangyue.constant.DelFlagConstant;
import org.jeecg.modules.wangyue.constant.IsPublishConstant;
import org.jeecg.modules.wangyue.constant.RoleConstant;
import org.jeecg.modules.wangyue.entity.SystemInfo;
import org.jeecg.modules.wangyue.entity.SystemInfoJs;
import org.jeecg.modules.wangyue.entity.UserBasic;
import org.jeecg.modules.wangyue.model.req.ReqCommonModel;
import org.jeecg.modules.wangyue.service.ISystemInfoJsService;
import org.jeecg.modules.wangyue.service.ISystemInfoService;
import org.jeecg.modules.wangyue.service.IUserBasicService;
import org.jeecg.modules.wangyue.utils.CommonUtil;
import org.jeecg.modules.wangyue.utils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.management.relation.Role;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
* @Description: 消息配置
* @Author: jeecg-boot
* @Date:   2023-06-30
* @Version: V1.0
*/
@Slf4j
@Api(tags="消息配置")
@RestController
@RequestMapping("/wangyue/sys/systemInfo")
public class SystemInfoManagerController extends JeecgController<SystemInfo, ISystemInfoService> {
   @Autowired
   private ISystemInfoService systemInfoService;


   /**
    * 消息配置-分页列表查询
    *
    * @param pageNo
    * @param pageSize
    * @return
    */
   @AutoLog(value = "消息配置-分页列表查询")
   @ApiOperation(value="消息配置-分页列表查询", notes="消息配置-分页列表查询")
   @GetMapping(value = "/list")
   public Result<?> queryPageList(@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                  @RequestParam(name="pageSize", defaultValue="10") Integer pageSize) {
       QueryWrapper<SystemInfo> queryWrapper = new QueryWrapper<>();
       queryWrapper.eq("del_flag", DelFlagConstant.normal);
       queryWrapper.orderByDesc("update_time").orderByDesc("create_time");
       Page<SystemInfo> page = new Page<SystemInfo>(pageNo, pageSize);
       IPage<SystemInfo> pageList = systemInfoService.page(page, queryWrapper);
       return Result.OK(pageList);
   }


   /**
    * 消息配置-添加或修改
    *
    * @param systemInfo
    * @return
    */
   @AutoLog(value = "消息配置-添加或修改")
   @ApiOperation(value="消息配置-添加或修改", notes="消息配置-添加或修改")
   @PostMapping(value = "/addOrEdit")
   public Result<?> addOrEdit(@RequestBody SystemInfo systemInfo) {
       if(oConvertUtils.isEmpty(systemInfo.getId())){ //如果是新增
           if(oConvertUtils.isEmpty(systemInfo.getPublishTo())){
               systemInfo.setPublishTo(RoleConstant.all); //如果是新增，并且发送对象没有传值，则默认为全部
           }
       }

       if(systemInfoService.saveOrUpdate(systemInfo)){
           return Result.OK("添加或修改成功！");
       }
       return Result.error("添加或修改失败！");
   }


    /**
     * 消息配置-通过id删除
     *
     * @param reqCommonModel
     * @return
     */
    @AutoLog(value = "消息配置-通过id删除")
    @ApiOperation(value="消息配置-通过id删除", notes="消息配置-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@Valid @RequestBody ReqCommonModel reqCommonModel) {
        try{
            systemInfoService.removeById(reqCommonModel.getId());
            return Result.OK("删除成功");
        }catch (Exception e){
            e.printStackTrace();
            return Result.error("删除失败");
        }

    }


    /**
     * 消息配置-发布
     *
     * @param systemInfo
     * @return
     */
    @AutoLog(value = "消息配置-发布")
    @ApiOperation(value="消息配置-发布", notes="消息配置-发布")
    @PostMapping(value = "/publish")
    public Result<?> publish(@RequestBody SystemInfo systemInfo) {
        if(oConvertUtils.isEmpty(systemInfo.getId()) || oConvertUtils.isEmpty(systemInfo.getPublishTo())){
            return Result.error("必输项不能为空");
        }

        if(systemInfoService.publishSystemInfo(systemInfo)){
            return Result.OK("发布成功！");
        }
        return Result.error("发布失败！");
    }


}
